package cn.iocoder.yudao.module.trade.controller.admin.depositorder;

import cn.iocoder.yudao.framework.common.util.CollectionUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.trade.controller.admin.depositorder.vo.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.depositorder.DepositOrderDO;
import cn.iocoder.yudao.module.trade.service.depositorder.DepositOrderService;

@Tag(name = "管理后台 - 押金信息")
@RestController
@RequestMapping("/trade/deposit-order")
@Validated
@Slf4j
public class DepositOrderController {

    @Resource
    private DepositOrderService depositOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建押金信息")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order:create')")
    public CommonResult<Long> createDepositOrder(@Valid @RequestBody DepositOrderSaveReqVO createReqVO) {
        return success(depositOrderService.createDepositOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新押金信息")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order:update')")
    public CommonResult<Boolean> updateDepositOrder(@Valid @RequestBody DepositOrderSaveReqVO updateReqVO) {
        depositOrderService.updateDepositOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除押金信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:deposit-order:delete')")
    public CommonResult<Boolean> deleteDepositOrder(@RequestParam("id") Long id) {
        depositOrderService.deleteDepositOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得押金信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order:get')")
    public CommonResult<DepositOrderRespVO> getDepositOrder(@RequestParam("id") Long id) {
        DepositOrderRespVO depositOrder = depositOrderService.getDepositOrder(id);
        log.info("getDepositOrder 1111:"+ JSON.toJSONString(depositOrder));
        return success(depositOrder);
    }
    @GetMapping("/get/deposit/return")
    @Operation(summary = "获得押金退瓶信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order:query')")
    public CommonResult<DepositOrderRespVO> getDeposit(@RequestParam("id") Long id) {
        DepositOrderRespVO depositOrder = depositOrderService.getDepositOrderAndRelanReturn(id);
        return success(depositOrder);
    }
    @GetMapping("/get/deposit/returnLog")
    @Operation(summary = "获得押金退瓶信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order:query')")
    public CommonResult<DepositOrderRespVO> getDepositLog(@RequestParam("id") Long id) {
        DepositOrderRespVO depositOrder = depositOrderService.getDepositOrderAndRelanReturnLog(id);
        return success(depositOrder);
    }

    @GetMapping("/page")
    @Operation(summary = "获得押金信息分页")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order:query')")
    public CommonResult<PageResult<DepositOrderPageReqVO>> getDepositOrderPage(@Valid DepositOrderPageReqVO pageReqVO) {
        if (!CollectionUtil.isEmpty(pageReqVO.getStatusList())) {
            // 用于前端
            pageReqVO.setDepositStatus(null);
        }
        PageResult<DepositOrderPageReqVO> pageResult = depositOrderService.getDepositOrderPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出押金信息 Excel")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order:export')")
    @OperateLog(type = EXPORT)
    public void exportDepositOrderExcel(@Valid DepositOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DepositOrderPageReqVO> list = depositOrderService.getDepositOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "押金信息.xls", "数据", DepositOrderRespVO.class,
                        BeanUtils.toBean(list, DepositOrderRespVO.class));
    }

}